package cn.com.zero.front.auth.controller;

import cn.com.zero.base.vo.ResponseVO;
import cn.com.zero.common.auth.util.AuthUtil;
import cn.com.zero.front.auth.po.AuthenticationApplyPO;
import cn.com.zero.front.auth.service.IRealNameVerifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Calendar;
import java.util.Date;

/**
 * @author xiongxj
 * @version 1.0.0
 * @Description 实名认证controller
 * @createTime 2023/3/16 14:22
 */
@Validated
@RestController
@Api(tags = "实名认证接口")
@RequestMapping("/front/auth/realNameVerify")
public class RealNameVerifyController {

    private IRealNameVerifyService realNameVerifyService;


    @ApiOperation(value = "验证证件号码是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "cardNo", required = true, value = "证件号码"),
            @ApiImplicitParam(paramType = "query", name = "cardType", required = true, value = "证件类型 1：身份证")

    })
    @GetMapping("/isExistCardNo/{cardNo}")
    public ResponseVO isExistCardNo(@PathVariable("cardNo") String cardNo,
                                    @RequestParam("cardType") Integer cardType) {
        boolean isExistCardNo = realNameVerifyService.isExistCardNo(cardNo, cardType);
        return ResponseVO.success(isExistCardNo);
    }

    @ApiOperation(value = "上传实名申请证件照，返回key值对一个信息：成功上传后返回地址")
    @ApiImplicitParam(paramType = "query", name = "idPhoto", value = "证件照")
    @PostMapping("/uploadApplyPhoto")
    public ResponseVO uploadApplyInfo(@RequestParam("idPhoto") MultipartFile idPhoto) {
        return ResponseVO.success(realNameVerifyService.uploadAuthenticationApply(idPhoto));
    }

    @ApiOperation(value = "用户填写实名申请信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "realName", required = true, value = "真实姓名; 不允许为空"),
            @ApiImplicitParam(paramType = "query", name = "cardType", required = true, value = "证件类型; 不允许为空"),
            @ApiImplicitParam(paramType = "query", name = "cardNo", required = true, value = "证件号码; 不允许为空"),
            @ApiImplicitParam(paramType = "query", name = "validity", value = "证件有效期; 允许为空，默认永久有效，格式：yyyy-MM-dd"),
            @ApiImplicitParam(paramType = "query", name = "frontDocument", value = "证件照(正面)地址; 不允许为空"),
            @ApiImplicitParam(paramType = "query", name = "backDocument", value = "证件照(反面)地址; 不允许为空"),
            @ApiImplicitParam(paramType = "query", name = "handDocument", value = "证件照(手持)地址; 不允许为空")
    })
    @PostMapping("/apply")
    public void apply(@RequestParam("realName") String realName,
                      @RequestParam("cardType") Integer cardType, @RequestParam("cardNo") String cardNo,
                      @RequestParam(value = "validity", required = false) Date validity,
                      @RequestParam(value = "frontDocument", required = false) String frontDocument,
                      @RequestParam(value = "backDocument", required = false) String backDocument,
                      @RequestParam(value = "handDocument", required = false) String handDocument) {
        if (validity == null) {
            // 默认永久有效
            Calendar instance = Calendar.getInstance();
            instance.add(Calendar.YEAR, 100);
            validity = instance.getTime();
        }
        // 调用执行添加客户实名申请信息
        String userId = AuthUtil.getUserIdFromRequest();
        realNameVerifyService.addAuthenticationApply(userId, realName, cardType, cardNo, validity, frontDocument, backDocument, handDocument);
    }


    @ApiOperation("查询最新的用户实名申请信息")
    @GetMapping("/info")
    public AuthenticationApplyPO queryAuthenticationApply() {
        String userId = AuthUtil.getUserIdFromRequest();
        return realNameVerifyService.queryAuthenticationApply(userId);
    }

    @ApiOperation(value = "修改实名信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "frontDocument", required = true, value = "证件照(正面)地址; 不允许为空"),
            @ApiImplicitParam(paramType = "query", name = "backDocument", required = true, value = "证件照(反面)地址; 不允许为空"),
            @ApiImplicitParam(paramType = "query", name = "handDocument", value = "证件照(手持)地址; 允许为空"),
            @ApiImplicitParam(paramType = "query", name = "validity", required = true, value = "证件有效期; 不允许为空，格式：yyyy-MM-dd")

    })
    @PostMapping("/update")
    public void update(@RequestParam("frontDocument") String frontDocument,
                       @RequestParam("backDocument") String backDocument,
                       @RequestParam(value = "handDocument", required = false) String handDocument,
                       @RequestParam(value = "validity") Date validity) {
        // 调用执行 修改客户实名信息
        String userId = AuthUtil.getUserIdFromRequest();
        realNameVerifyService.updateAuthenticationInfo(userId, validity, frontDocument, backDocument, handDocument);
    }


    @Autowired
    public void setRealNameVerifyService(IRealNameVerifyService realNameVerifyService) {
        this.realNameVerifyService = realNameVerifyService;
    }
}
